﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FrenzDenWebSite.Business;
using System.Web.UI.HtmlControls;

namespace FrenzDenWebSite
{
    public partial class EducationWorkForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies[SiteConstants.COOKIESTRINGPROFILE] == null)
            {
                Response.Redirect("~/Default.aspx?prev=EducationWork&showmsg=1");
            }

            if (!IsPostBack)
            {
                LoadYear();
                long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                GetEmployers(profileId);
                GetEducation(profileId);
                GetProfileWorkspace(profileId);
            }
        }

        private void LoadYear()
        {
            int endYear = DateTime.Now.Year;
            int startYear = endYear - 80;

            for (int i = endYear; i >= startYear; i--)
            {
                ddlStartYear.Items.Add(i.ToString());
                ddlEndYear.Items.Add(i.ToString());
            }
        }

        private void GetEmployers(long profileId)
        {
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                GridEmployers.DataSource = dataContext.GetProfileEmployer(profileId);
                GridEmployers.DataBind();
            }
        }

        private void GetEducation(long profileId)
        {
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                GridColleges.DataSource = dataContext.GetProfileEducation(profileId);
                GridColleges.DataBind();
            }
        }

        private void GetProfileWorkspace(long profileId)
        {
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                var result = dataContext.GetProfileWorkspace(profileId);
                foreach (var item in result)
                {
                    ddlEducationPrivacy.SelectedValue = item.EducationPrivacy.ToString();
                    ddlEmployerPrivacy.SelectedValue = item.EmployerPrivacy.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                    dataContext.UpdateProfileWorkplace(profileId, Convert.ToInt16(ddlEmployerPrivacy.SelectedValue), Convert.ToInt16(ddlEducationPrivacy.SelectedValue));
                    GetEmployers(profileId);
                    GetEducation(profileId);
                    GetProfileWorkspace(profileId);
                    msgbox = new MessageBox("Successfully updated!");
                }
            }
            catch
            {
                msgbox = new MessageBox(SiteConstants.ERRORMSG);
            }

            msgboxpanel.InnerHtml = msgbox.ReturnObject();
        }

        protected void btnAddEmployer_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    DateTime startDate = DateTime.MinValue;
                    DateTime endDate = DateTime.MinValue;
                    long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                    if (ddlStartMonth.SelectedValue != "" && ddlStartYear.SelectedValue != "")
                    {
                        startDate = Convert.ToDateTime(string.Format("{0}-{1}-01", ddlStartYear.SelectedValue, ddlStartMonth.SelectedValue));
                    }

                    if (ddlEndMonth.SelectedValue != "" && ddlEndYear.SelectedValue != "")
                    {
                        endDate = Convert.ToDateTime(string.Format("{0}-{1}-01", ddlEndYear.SelectedValue, ddlEndMonth.SelectedValue));
                    }

                    dataContext.AddEmployer(profileId, txtCompanyName.Text, txtCompanyCity.Text, startDate, endDate);
                    GetEmployers(profileId);
                }
            }
            catch
            {
                msgbox = new MessageBox(SiteConstants.ERRORMSG);
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
        }

        protected void imgDeleteEmployer_Click(object sender, ImageClickEventArgs e)
        {
            long pid = Convert.ToInt64((sender as ImageButton).Attributes["pid"]);
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                dataContext.RemoveProfileEmployer(pid);
                GetEmployers(profileId);
            }
        }

        protected void btnAddCollege_Click(object sender, EventArgs e)
        {
            MessageBox msgbox = null;
            try
            {
                using (DenStoreDataContext dataContext = new DenStoreDataContext())
                {
                    long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;

                    dataContext.AddEducation(profileId, txtCollegeName.Text, txtCollegeCity.Text);
                    GetEducation(profileId);
                }
            }
            catch
            {
                msgbox = new MessageBox(SiteConstants.ERRORMSG);
                (this.Master.FindControl("msgboxpanel") as HtmlGenericControl).InnerHtml = msgbox.ReturnObject();
            }
        }

        protected void imgDeleteCollege_Click(object sender, ImageClickEventArgs e)
        {
            long pid = Convert.ToInt64((sender as ImageButton).Attributes["pid"]);
            using (DenStoreDataContext dataContext = new DenStoreDataContext())
            {
                long profileId = SiteCommon.DecryptSession(Request.Cookies[SiteConstants.COOKIESTRINGPROFILE].Value).ProfileID;
                dataContext.RemoveProfileEducation(pid);
                GetEducation(profileId);
            }
        }
    }
}